﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 菜单相关
    /// </summary>
    public class MenuInfoController : Controller
    {
        IMenuInfoBll _menuInfoBll;
        IUserInfoBll _userInfoBll;

        public MenuInfoController(IMenuInfoBll menuInfoBll, IUserInfoBll userInfoBll)
        {
            _menuInfoBll = menuInfoBll;
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 菜单列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult MenuInfoListView()
        {
            return View();
        }

        /// <summary>
        /// 添加菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateMenuInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateMenuInfoView()
        {
            return View();
        }

        /// <summary>
        /// 获取首页菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetHomeMenu()
        {
            List<HomeMenuInfoModel> menuList = new List<HomeMenuInfoModel>();

            string userId = HttpContext.Session.GetString("UserId");

            //用户id有值的时候才去数据库查菜单数据
            if (!string.IsNullOrEmpty(userId))
            {
                //调用数据访问层，获取数据库中的菜单数据
                menuList = _menuInfoBll.GetHomeMenu(userId);
            }

            return new
            {
                homeInfo = new
                {
                    title = "首页",
                    href = "/Home/NoticeIndex"
                },
                logoInfo = new
                {
                    title = "家园装饰系统",
                    image = "/images/logo.png",
                    href = ""
                },
                menuInfo = new List<HomeMenuInfoModel>()
                {
                    new HomeMenuInfoModel()
                    {
                      Title = "常规管理",
                      Icon = "fa fa-address-book",
                      Href = "",
                      Target = "_self",
                      Child = menuList
                      //Child = new List<HomeMenuInfoModel>()
                      //{
                      //    new HomeMenuInfoModel()
                      //    {
                      //        Title = "用户管理",
                      //        Href = "/UserInfo/UserInfoListView",
                      //        Icon = "fa fa-window-maximize",
                      //        Target = "_self"
                      //    },
                      //    new HomeMenuInfoModel()
                      //    {
                      //        Title = "部门管理",
                      //        Href = "/DepartmentInfo/DepartmentInfoListView",
                      //        Icon = "fa fa-window-maximize",
                      //        Target = "_self"
                      //    },
                      //}
                    }
                }
            };
        }


        /// <summary>
        /// 根据菜单id获取菜单信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetMenuInfoById(string menuId)
        {
            MenuInfo MenuInfo = _menuInfoBll.GetMenuInfoById(menuId);

            if (MenuInfo == null)
            {
                return MyJsonResult.GetErrorResult("菜单不存在");
            }
            else
            {
                //获取菜单下拉选
                var optionData = _menuInfoBll.GetSelectOptionList(menuId);

                return MyJsonResult.GetSuccessResult(new   //mapping
                {
                    MenuInfo,
                    optionData
                });
            }
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateMenuInfo(string menuId, string title, string description, int level, int sort, string href, string icon, string target, string parentId)
        {
            //参数验证
            if (string.IsNullOrEmpty(menuId))
            {
                return MyJsonResult.GetErrorResult("菜单id不能为空");
            }
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("菜单名不能为空");
            }

            string msg;
            //调用业务逻辑层
            bool isSuccess = _menuInfoBll.UpdateMenuInfo(menuId, title, description, level, sort, href, icon, target, parentId, out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取菜单列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetMenuInfoList(string menuName, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查菜单数据
            var list = _menuInfoBll.GetMenuInfoList(menuName, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateMenuInfo(string title, string description, int level, int sort, string href, string icon, string target, string parentId)
        {
            //参数验证
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("请输入菜单标题");
            }

            //创建业务逻辑层
            //MenuInfoBll MenuInfoBll = new MenuInfoBll();

            string msg;
            bool isSuccess = _menuInfoBll.CreateMenuInfo(title, description, level, sort, href, icon, target, parentId, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 获取下拉选备选数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetSelectOptionList(string roleId, string menuId)
        {
            //获取下拉选备选数据
            var list = _menuInfoBll.GetSelectOptionList(menuId);
            return MyJsonResult.GetSuccessResult(new
            {
                list
            });
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteMenuInfo(string menuId)
        {
            if (string.IsNullOrEmpty(menuId))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _menuInfoBll.DeleteMenuInfo(menuId);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteMenuInfos(List<string> menuIds)
        {
            if (menuIds.Count <= 0 || menuIds == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _menuInfoBll.DeleteMenuInfos(menuIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

    }

}
